/**
 * @file LoopFIFO.h
 * @brief
 * @author xuesong (wenshaoliu@foxmail.com)
 * @version 1.0
 * @date 2024-08-20
 *
 * @copyright Copyright (c) 2024  by xuesong
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-08-20     <td>1.0     <td>xuesong   <td>内容
 * </table>
 */
#ifndef __LOOP_FIFO__H__
#define __LOOP_FIFO__H__

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>
#include <stddef.h>
#include <string.h>

typedef enum
{
    Fifo_NO_ERR = 0,        // not anything error.
    Fifo_SPACE_NOT_ENOUGH,  // space not enough
    Fifo_NOT_ALIGNMENT,     // stream buffer not alignment.
    Fifo_BUFFER_IS_INVALID, // Stream buffer is invalid.
    Fifo_DYNAMIC_MEM_ERROR,
} FifoStatusDef;

typedef struct
{
    union
    {
        struct
        {
            unsigned char Init : 1; //
            unsigned char Full : 1; //
        } Bits;
        uint8_t Byte;
    } Flag;
    uint16_t wrIndex;
    uint16_t rdIndex;
    uint16_t bufferSize;
    uint8_t *pcBuffer;
} fifo_data_t;
//--------------------------------------------------------------------------
/**
 * @brief
 * @param  pxFifo           fifo buffer object
 * @param  pcBuff           buffer
 * @param  usSize           buffer size
 * @return FifoStatusDef    result
 */
FifoStatusDef xFifoBufferStaticCreate(fifo_data_t *pxFifo, uint8_t *pcBuff, uint16_t usSize);
/**
 * @brief
 * @param  pxFifo           fifo buffer object
 * @param  usSize           buffer size
 * @return FifoStatusDef    result
 */
FifoStatusDef xFifoBufferCreate(fifo_data_t *pxFifo, uint16_t usSize);
/**
 * @brief
 * @param  pxFifo           fifo buffer object
 * @return FifoStatusDef
 */
FifoStatusDef xFifoBufferDestroy(fifo_data_t *pxFifo);
uint16_t getXFifoBufferOccupy(fifo_data_t *pxFifo);
uint16_t getXFifoBufferFree(fifo_data_t *pxFifo);
FifoStatusDef xFifoBufferWrite(fifo_data_t *pxFifo, uint8_t *pcBuff, uint16_t usSize);
FifoStatusDef xFifoBufferPeek(fifo_data_t *pxFifo, uint8_t *pcBuff, uint16_t offset, uint16_t usSize);
FifoStatusDef xFifoBufferRead2X(fifo_data_t *pxFifo, uint16_t usSize);
//--------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif

#endif
